// pages/shop/index.js
Page({
  data: {
    shops: [
      {
        id: 1,
        name: "老成都火锅",
        cover: "/assets/img/message/mountain.png",
        rating: 4.8,
        distance: "500m",
        category: "火锅",
        price: "¥80/人",
        address: "春熙路123号",
        phone: "028-12345678",
        tags: ["正宗", "人气", "老字号"],
        description: "正宗成都火锅，麻辣鲜香，环境优雅"
      },
      {
        id: 2,
        name: "星巴克咖啡",
        cover: "/assets/img/message/phone.png",
        rating: 4.5,
        distance: "200m",
        category: "咖啡",
        price: "¥35/人",
        address: "太古里1楼",
        phone: "028-87654321",
        tags: ["环境好", "WiFi", "适合办公"],
        description: "环境舒适，适合工作学习，咖啡品质稳定"
      },
      {
        id: 3,
        name: "海底捞",
        cover: "/assets/img/message/draw.png",
        rating: 4.9,
        distance: "800m",
        category: "火锅",
        price: "¥120/人",
        address: "银石广场3楼",
        phone: "028-11223344",
        tags: ["服务好", "排队", "网红"],
        description: "服务超棒，排队也要吃，体验感满分"
      },
      {
        id: 4,
        name: "肯德基",
        cover: "/assets/img/message/computer.png",
        rating: 4.2,
        distance: "300m",
        category: "快餐",
        price: "¥25/人",
        address: "春熙路步行街",
        phone: "028-55667788",
        tags: ["快捷", "24小时", "连锁"],
        description: "24小时营业，方便快捷，味道稳定"
      }
    ],
    categories: [
      { name: "全部", active: true },
      { name: "火锅", active: false },
      { name: "咖啡", active: false },
      { name: "快餐", active: false },
      { name: "中餐", active: false }
    ]
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '热门商家'
    })
  },

  onShopTap: function(e) {
    const shop = e.currentTarget.dataset.shop
    
    wx.showModal({
      title: shop.name,
      content: `评分：${shop.rating}⭐\n距离：${shop.distance}\n价格：${shop.price}\n地址：${shop.address}\n电话：${shop.phone}\n\n${shop.description}`,
      showCancel: true,
      cancelText: '关闭',
      confirmText: '查看详情',
      success: function(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: `/pages/shop-detail/index?id=${shop.id}`
          })
        }
      }
    })
  },

  onCategoryTap: function(e) {
    const index = e.currentTarget.dataset.index
    const categories = this.data.categories.map((item, i) => {
      item.active = i === index
      return item
    })
    this.setData({ categories })
    
    wx.showToast({
      title: '筛选：' + categories[index].name,
      icon: 'none'
    })
  },

  onCallTap: function(e) {
    e.stopPropagation()
    const phone = e.currentTarget.dataset.phone
    
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function() {
        wx.showToast({
          title: '正在拨打电话',
          icon: 'success'
        })
      },
      fail: function() {
        wx.showToast({
          title: '拨打电话失败',
          icon: 'none'
        })
      }
    })
  }
})
